<?php
require_once 'Zend/Controller/Action/Helper/Abstract.php';

class Custom_Controller_Action_Helper_Watermark extends Zend_Controller_Action_Helper_Abstract
{
 	
	public function watermark($source,$dir=null)
    {
    	//目录是否存在,不存在则生成目录
    	if($dir===null){
    		$dir=PROJECT_ROOT.'/data/uploads/picture/';
    	}
    	if (!is_dir($dir)){
	    	$folder=new Custom_Controller_Plugin_Folder();
	    	$folder->createDir($dir);
    	}
    	
		$suffix=strtolower(pathinfo($source, PATHINFO_EXTENSION));
        
        $imagename=substr(md5(time()),13,12).'.'.$suffix;
        $target=$dir.$imagename;
        $image	= new Custom_Controller_Plugin_Image($source);
        $imageinfo=$image->getImageInfo($source);
        list($width, $height) = $imageinfo;
        //如果图片过大,则产生缩略图片,再进行加水印
        if($width>1200||$height>1200){
        	//先生成缩略图片
        	$opthon=array(1200,1200);
        	$image->setThumbarray($opthon);
            $image->setThumbnailName($dir,$imagename);
            $source=$image->getThumbnail();
        }
       //再给图片加水印
        $image	= new Custom_Controller_Plugin_Image($source);
        $image->setWaterThumbnailName($dir,basename($source));
        $image->setWaterImage();
		$newname=$image->getWatermark();
			
        return $newname;
    }


}